#ifndef LISTVIEW_H
#define LISTVIEW_H

#include <QTreeView>

#include <QString>

class List;
class Item;
class Source;

class PriceDelegate;
class QuantityDelegate;

class DummyRow;
class ItemRow;
class SourceRow;

class QStandardItemModel;
class QStandardItem;

class QKeyEvent;


class ListView : public QTreeView
{
    Q_OBJECT

public:
    explicit ListView(QWidget *parent = 0);
    virtual ~ListView();

    void setList(List *list);

    Item *currentItem();
    Source *currentSource();

    void createItem();
    void createSource(const bool tryFromClipboard = true);

signals:
    void itemAppendable(bool);
    void itemRemovable(bool);

    void webSourceAppendable(bool);
    void userSourceAppendable(bool);

    void sourceRemovable(bool);

public slots:
    void resizeAllColumnsToContents();

    void select(Item *item);
    void select(Source *source);

protected:
    void cleanUp();

    void removeItem(const QModelIndex &index);

    void createSource(
        const QModelIndex &index
    ,   const bool tryFromClipboard = true);
    void deleteSource(const QModelIndex &index);

    void deleteItem(const QModelIndex &index);

    ItemRow *itemRow(const QModelIndex &index) const;
    ItemRow *itemRow(Item *item) const;
    ItemRow *itemByItemsDummyRow(const QModelIndex &index) const;

    SourceRow *sourceRow(const QModelIndex &index) const;
    SourceRow *sourceRow(Source *source) const;

    void setupNewModel();
    void fillUp();

    void connectToList(List *list);

    virtual void currentChanged(const QModelIndex &current, const QModelIndex &previous);

protected slots:
    void me_activated(const QModelIndex &index);
    void me_itemChanged(QStandardItem *item);

    void list_itemAdded(Item *item);
    void list_itemRemoved(Item *item);

    void checkIfWebSourceIsAppendable();

protected:
    virtual void keyPressEvent(QKeyEvent *event);

protected:
    List *m_list;

    QStandardItemModel *m_model;

    QSet<ItemRow*> m_itemRows;
    DummyRow *m_itemDummyRow;

    PriceDelegate *m_priceDelegate;
    QuantityDelegate *m_quantityDelegate;
};

#endif // LISTVIEW_H
